package com.ryzx.security.service;

import com.ryzx.auth.service.BapUserService;
import com.ryzx.auth.vo.BapPost;
import com.ryzx.auth.vo.BapUser;
import com.ryzx.security.entity.SecurityUserDetails;
import jakarta.annotation.Resource;
import jakarta.transaction.Transactional;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;


@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private BapUserService bapUserService;

    @Transactional
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        BapUser user = bapUserService.findByAccount(username);
        if (null == user){
            throw new UsernameNotFoundException("用户名不存在！");
        }
        BapPost post=user.getBapPost();
        HashMap<String,Object> auth=new HashMap<>();
        auth.put("api",new HashSet<>(post.getBapApiAuthSet()));
        auth.put("data",new HashSet<>(post.getBapDataAuthSet()));
        auth.put("menu",new HashSet<>(post.getBapMenuSet()));


        List<String> authorities = new ArrayList<>();
        SecurityUserDetails securityUser = new SecurityUserDetails(user);
        securityUser.setPermissionValueList(authorities);
        securityUser.setAuthSet(auth);
        return securityUser;
    }

}

